package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.bean.MessageListBean;
import com.fy.fyzf.presenter.MessagePresenter;
import com.fy.fyzf.view.MessageCallback;
import com.fy.fyzf.weight.SlideRecyclerView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.adapter.MessageAdapter;

public class MessageActivity extends BaseActivity<MessagePresenter> implements MessageCallback {


    @BindView(R.id.recycleView)
    SlideRecyclerView recycleView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    private MessageAdapter messageAdapter;

    @Override
    protected MessagePresenter createPresenter() {
        return new MessagePresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_message;
    }

    private int tag;
    private int page = 1;
    private int pageSize = 10;
    @Override
    public void initView() {
        super.initView();
        setTitleName("消息中心");
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                tag = 1;
                page = 1;
                initData();
            }
        });

        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                tag = 2;
                page++;
                initData();
            }
        });

        messageAdapter = new MessageAdapter(MessageActivity.this);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(messageAdapter);

        messageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<MessageListBean.ListBean> data = adapter.getData();
                mPresenter.deleteMsg(data.get(position).getMsgId());
                messageAdapter.removeItem(position);
            }
        });
        messageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<MessageListBean.ListBean> data = adapter.getData();
                Intent intent = new Intent(MessageActivity.this, MessageDetailActivity.class);
                intent.putExtra("data", data.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getMessageList(page,pageSize);
    }

    public void setReady(int id) {

        mPresenter.updateMsgForRead(id);
    }
    @Override
    public void getMessageSuccess(MessageListBean messageListBean) {
        smartRefresh.finishRefresh();
        smartRefresh.finishLoadMore();

        if (messageListBean.getList().size() == 0 || messageListBean == null) {
            messageAdapter.setEmptyView(R.layout.layout_nodata_msg);
        }
        if (tag == 1) {
            messageAdapter.setNewData(messageListBean.getList());
        } else {
            messageAdapter.addData(messageListBean.getList());
        }

    }
}